/**
 * 上传电子发票
 * 
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
PCL.define("PSI.SLN0001.Vat.IncomeImportForm", {
  extend: "PSI.AFX.Form.EditForm",

  config: {
    companyName: ""
  },

  /**
   * 初始化组件
   * 
   * @override
   */
  initComponent() {
    const me = this;

    const buttons = [];

    buttons.push({
      text: "上传电子发票",
      ...PSI.Const.BTN_STYLE,
      formBind: true,
      iconCls: "PSI-button-ok",
      handler() {
        me._onOK();
      },
      scope: me
    }, {
      text: "取消",
      ...PSI.Const.BTN_STYLE,
      handler() {
        me.close();
      },
      scope: me
    });

    PCL.apply(me, {
      header: {
        title: me.formatTitle(PSI.Const.PROD_NAME),
        height: 40
      },
      width: 512,
      height: 210,
      layout: "fit",
      items: [{
        id: me.buildId(me, "importForm"),
        xtype: "form",
        border: 0,
        layout: {
          type: "table",
          columns: 1,
          tableAttrs: PSI.Const.TABLE_LAYOUT,
        },
        height: "100%",
        bodyPadding: 5,
        fieldDefaults: {
          labelWidth: 60,
          labelAlign: "right",
          labelSeparator: "",
          msgTarget: 'side'
        },
        items: [{
          xtype: "hidden",
          name: "id",
          value: me.getEntity().get("id"),
        }, {
          xtype: "displayfield",
          fieldLabel: "组织机构",
          labelWidth: 70,
          value: me.toFieldNoteText(me.getCompanyName()),
        }, {
          xtype: "displayfield",
          fieldLabel: "发票号",
          labelWidth: 70,
          value: me.toFieldNoteText(me.getEntity().get("ref")),
        }, {
          xtype: 'filefield',
          name: 'vat_income_file',
          afterLabelTextTpl: '<span style="color:red;font-weight:bold" data-qtip="必需填写">*</span>',
          fieldLabel: '电子发票',
          labelWidth: 70,
          width: 480,
          msgTarget: 'side',
          allowBlank: false,
          anchor: '100%',
          buttonText: '选择电子发票'
        }],
        buttons,
      }],
      listeners: {
        close: {
          fn: me._onWndClose,
          scope: me
        },
        show: {
          fn: me._onWndShow,
          scope: me
        }
      }
    });

    me.callParent(arguments);

    me.importForm = PCL.getCmp(me.buildId(me, "importForm"));
  },

  /**
   * @private
   */
  _onOK() {
    const me = this;

    const f = me.importForm;
    const el = f.getEl();
    el && el.mask('正在上传...');
    f.submit({
      url: me.URL("SLN0001/Vat/uploadIncomePDF"),
      method: "POST",
      success(form, action) {
        el && el.unmask();

        me.showInfo("成功上传电子发票");

        me.close();
        const parentForm = me.getParentForm();
        if (parentForm && parentForm.refreshMainGrid) {
          parentForm.refreshMainGrid.apply(parentForm, [me.getEntity().get("id")]);
        }
      },
      failure(form, action) {
        el && el.unmask();
        me.showInfo(action.result.msg);
      }
    });
  },

  /**
   * @private
   */
  _onWndShow() {
    const me = this;

    const parentForm = me.getParentForm();
    if (parentForm) {
      parentForm._keyMapMain.disable();
    }
  },

  /**
   * @private
   */
  _onWndClose() {
    const me = this;

    const parentForm = me.getParentForm();
    if (parentForm) {
      parentForm._keyMapMain.enable();
    }
  },
});
